package com.example.myapplication.slice;

import com.example.myapplication.ResourceTable;
import com.example.myapplication.entity.DataItem;
import com.example.myapplication.provider.DataItemProvider;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

import java.util.ArrayList;

public class MainAbilitySlice extends AbilitySlice {
    private static final int LIST_ITEM_COUNT = 20;

    private ListContainer listContainer;

    private DataItemProvider dataItemProvider;

    private ArrayList<DataItem> dataItems;


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        /*顶部切换控件 tablist*/
        TabList tabList = (TabList) findComponentById(ResourceTable.Id_tab_list);
        TabList.Tab tab1 = tabList.new Tab(getContext());
        tab1.setText("首页  ");
        tabList.addTab(tab1);
        TabList.Tab tab2 = tabList.new Tab(getContext());
        tab2.setText("  登录  ");
        tabList.addTab(tab2);
        AbilitySlice slice=this;//指向当前对象
        //添加tablist的选择效果
        tabList.addTabSelectedListener(new TabList.TabSelectedListener() {
            ComponentContainer componentContainer = (ComponentContainer) findComponentById(ResourceTable.Id_tab_container);
            @Override
            public void onSelected(TabList.Tab tab) {
                if(tab.getText().equals("首页  ")){//当点首页时显示首页
                    componentContainer.removeAllComponents();//清除当前界面
                    Component hello = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_hello,null,false);
                    componentContainer.addComponent(hello);//显示首页
                }else  if(tab.getText().equals("  登录  ")){//当点登录时显示登录界面
                    componentContainer.removeAllComponents();//清除当前界面
                    Component login = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_login,null,false);
                    componentContainer.addComponent(login);//显示登录界面
                    Button button = (Button) findComponentById(ResourceTable.Id_loginbutton);
                    button.setClickedListener(new Component.ClickedListener() {//添加按钮的选择效果
                        @Override
                        public void onClick(Component component) {
                            /*获取帐号和密码*/
                            Text username = (Text) findComponentById(ResourceTable.Id_username);
                            Text password = (Text) findComponentById(ResourceTable.Id_password);
                            if(username.getText().trim().equals("")&&password.getText().trim().equals("")){ //判断帐号和密码是否匹配
                                componentContainer.removeAllComponents();
                                MainAbilitySlice.super.setUIContent(ResourceTable.Layout_main_layout);
                                initComponents();
                                initDataItem();
                                initProvider();
                            }else{
                                componentContainer.removeAllComponents();
                                componentContainer.addComponent(login);
                            }
                        }
                    });
                }
            }

            @Override
            public void onUnselected(TabList.Tab tab) {
            }

            @Override
            public void onReselected(TabList.Tab tab) {
            }
        });
        tabList.selectTab(tab1);
    }

    private void initComponents() {
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_list_container);
    }

    private void initProvider() {
        dataItemProvider = new DataItemProvider(this, dataItems);
        listContainer.setItemProvider(dataItemProvider);
        listContainer.setReboundEffect(true);
    }

    private void addAndUpdateDataItem(int index) {
        addDataItem(index);
        dataItemProvider.update(dataItems);
    }

    private void initDataItem() {
        dataItems = new ArrayList<>();
        for (int i = 0; i < LIST_ITEM_COUNT; i++) {
            addDataItem(i);
        }
    }

    private void addDataItem(int index) {
        dataItems.add(new DataItem("用户" + index, "消息" + index));
    }


}


